<? if ($c = stack('name')) {

$s = component_settings($c);
	
$now = parse_today();
$expires = date("Y-m-d", mktime(0,0,0, $now['m'], $now['d'] + (int)$s['new_user_expires'], $now['y']));;

$_record = array('kind' => TK_NEW_USER, 'expires' => $expires);

$_values	= array('firstname', 'lastname', 'middlename', 'position', 'fullname', 'ownership', 'edrpou', 'certificatenumber', 'low_index', 'low_geo_id', 'low_address', 'fact_index', 'fact_geo_id', 'fact_address', 'contactphone', 'contactphone2');

$_values_session = array('persontype', 'email', 'pass');

$_POST['contactphone'] = $_POST['contactphone_pref'].'-'.$_POST['contactphone'];
$_POST['contactphone2'] = $_POST['contactphone2_pref'].'-'.$_POST['contactphone2'];

$check = $_POST; foreach($_values_session as $v) $check[$v] = $_SESSION['reg'][$v];

//debug($check);
$val = gccPersonValidate($check);
//debug($val);

//dbdebug();

// debug($val, 'val');

if ($val['validated']) {

	$uuid = gccGetUUID();



	
// ** crafting ticket
	
	$res = array();
	foreach($_values as $v) $res[$v] = postdata($v);
	foreach($_values_session as $v) $res[$v] = $_SESSION['reg'][$v];
	$res = php2js($res);	

	$recp = 	$_SESSION['reg']['email'];
	
	datafetch('db_insert', 'cs_ticket', array_merge($_record, array('id' => $uuid, 'data' => $res)));




// ** mail notification

	$tpl_tags = array(
		'expires' => date2str(parse_date($expires), context('lang')),
		'ticket_link' => "http://$_SERVER[SERVER_NAME]:$_SERVER[SERVER_PORT]/$CONTEXT[lang_prefix]register/confirm-mail/id-$uuid/",
		'base_url' => "$_SERVER[SERVER_NAME]:$_SERVER[SERVER_PORT]",
		'email' => $recp,
	);

	$se = array(); $re = array();	foreach($tpl_tags as $k => $v) { $se[] = '{'.$k.'}'; $re[] = $v; }

	$t = str_replace($se, $re, $s['mail_confirmation']);


	try {
		
		$subject = '=?utf-8?B?'.base64_encode( stripslashes($s['mail_confirmation_subj'])).'?=';
		
		$random_hash = md5(date('r', time())); 
		
		$headers = "From: \"=?utf-8?B?".base64_encode( stripslashes($s['mail_sender_name']))."?=\" <$s[mail_sender]>";
	
		$headers .= "\nX-Mailer: PHP/".phpversion();
		$headers .= "\nMIME-Version: 1.0";
		$headers .= "\nContent-Type: multipart/mixed; boundary=\"mixed-".$random_hash."\"";
		$headers .= "\nContent-Transfer-Encoding: base64";
	
		$body = chunk_split(base64_encode(stripslashes($t)));
	
	
		ob_start();

?>
--mixed-<?=$random_hash?>

Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: base64

<?=$body?>

--mixed-<?=$random_hash?>--

<?
	
		$body = ob_get_clean();
		
		if (@mail($recp, $subject, $body, $headers, '-f '.stripslashes($s['mail_sender'])))
			diags('<p class="success">Sent</p>');
		else
			throw new Exception('Not sent');
			
	//			exit( str_replace('<', '&lt;', json_encode(array("diags" => implode('', $GLOBALS['DIAGS'])))) );
	
	} catch(Exception $e) {
		diags('<p class="error">'.$e->getMessage().'</p>');
	}




	
	$_SESSION['reg_sent'] = array('email' => $_SESSION['reg']['email'], 'expires' => date2str(parse_date($expires), context('lang')));
	$_SESSION['reg'] = false;	// registration done




	$res = array();
	foreach($_values as $v)
		$res[] = array($v, true, '');
	
	print(json_encode($res));
	exit;

} else {

// debug('not validated');	
/*	
	$r = array();

	$res = $val->GetErrors();
	foreach($res as $f => $e)
		if (isset($r[$f]))
			$r[$f][2] .= " $e";
		else
			$r[$f] = array($f, false, $e);
	
	$res = array();
	foreach($r as $v)
		$res[] = $v;
*/	
	print($val['raw']);
	exit;
}

unstack();
}